//邮箱
function isEmail(s) {
	return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
		s
	);
}
//手机号
function isMobile(s) {
	return /^1[0-9]{10}$/.test(s);
}
//电话号码
function isPhone(s) {
	return /^(0\d{2,3}-){0,1}\d{7,8}$/.test(s);
}
//电话号码加手机号
function isTel(value) {
	var length = value.length;
	// var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
	var mobile = /^1[0-9]{10}$/;
	// var tel = /^(\d{3,4}-?)?\d{7,9}$/g;
	var tel = /^(\d{3,4}-)?\d{7,9}$/g;
	return tel.test(value) || (length === 11 && mobile.test(value));
}
//联系电话 （手机号、座机号）
function PhoneMobile(s) {
	return /^1[3|4|5|6|7|8|9][0-9]\d{8}$|^(0\d{2,3}-){0,1}\d{7,8}$/.test(s);
}
//匹配qq
function isQq(value) {
	return /^[1-9]\d{4,12}$/.test(value);
}
//邮编
function isZipCode(value) {
	var zip = /^[0-9]{6}$/;
	return zip.test(value);
}

//URL地址
function isURL(s) {
	return /^http[s]?:\/\/.*/.test(s);
}
//IP地址验证
function ip(value) {
	return /^(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.)(([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))\.){2}([1-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5])))$/.test(
		value
	);
}
//小写字母
function isUpperCase(str) {
	const reg = /^[A-Z]+$/;
	return reg.test(str);
}
//大小写字母
function isAlphabets(str) {
	const reg = /^[A-Za-z]+$/;
	return reg.test(str);
}
/*
 * 字符验证，只能包含中文、英文、数字、下划线等字符。
 * 只能包含中文、英文、数字、下划线等字符
 */
function stringCheck(value) {
	return /^[a-zA-Z0-9\u4e00-\u9fa5-_]+$/.test(value);
}
//匹配汉字
function isChinese(value) {
	return /^[\u4e00-\u9fa5]+$/.test(value);
}
//匹配中文(包括汉字和字符)
function isChineseChar(value) {
	return /^[\u0391-\uFFE5]+$/.test(value);
}
//是否是PC
function isPc() {
	const userAgentInfo = navigator.userAgent;
  console.log("userAgentInfo: ",userAgentInfo);
	const Agents = [
		"Android",
		"iPhone",
		"SymbianOS",
		"Windows Phone",
		"iPad",
		"iPod",
	];
	let flag = true;
	for (let v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false;
			break;
		}
	}
	return flag;
};

//判断身份证号码
function isCardId(code) {
	let msg = "";
	const city = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江 ",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北 ",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏 ",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门",
		91: "国外 ",
	};
	if (!isNull(code)) {
		if (code.length === 18) {
			if (!code || !/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(code)) {
				msg = "证件号码格式错误";
				return false;
			} else if (!city[code.substr(0, 2)]) {
				msg = "地址编码错误";
				return false;
			} else {
				// 18位身份证需要验证最后一位校验位
				code = code.split("");
				// ∑(ai×Wi)(mod 11)
				// 加权因子
				let factor = [
					7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,
				];
				// 校验位
				let parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2, "x"];
				let sum = 0;
				let ai = 0;
				let wi = 0;
				for (let i = 0; i < 17; i++) {
					ai = code[i];
					wi = factor[i];
					sum += ai * wi;
				}
				let last = parity[sum % 11];
				if ("" + last !== "" + code[17]) {
					msg = "证件号码校验位错误";
					return false;
				}
			}
		} else {
			msg = "证件号码长度不为18位";
			return false;
		}
	} else {
		msg = "证件号码不能为空";
		return false;
	}
	if (msg) {
		console.log(msg);
	}
	return true;
}
//是否为整数
function isInteger(value) {
	// eslint-disable-next-line no-useless-escape
	return /^[-\+]?\d+$/.test(value) && parseInt(value) >= 0;
}
//是否为整数，并且大于=0
function isIntegerNoZero(value) {
	// eslint-disable-next-line no-useless-escape
	return /^[-\+]?\d+$/.test(value) && parseInt(value) > 0;
}
//判断数值类型，包括整数和浮点数
function isNumber(value) {
	// eslint-disable-next-line no-useless-escape
	return /^[-\+]?\d+$/.test(value) || /^[-\+]?\d+(\.\d+)?$/.test(value);
}
//只能输入[0-9]数字
function isDigits(value) {
	return /^\d+$/.test(value);
}

/*
 * 匹配密码，以字母开头，长度在6-12之间，只能包含字符、数字和下划线。
 * 以字母开头，长度在6-12之间，只能包含字符、数字和下划线。
 */
function isPwd(value) {
	return /^[a-zA-Z]\\w{6,12}$/.test(value);
}

export default {
    isEmail,
    isMobile,
    isPhone,
    isTel,
    PhoneMobile,
    isQq,
    isZipCode,
    isURL,
    ip,
    isUpperCase,
    isAlphabets,
    stringCheck,
    isChinese,
    isChineseChar,
    isPc,
    isCardId,
    isInteger,
    isIntegerNoZero,
    isNumber,
    isDigits,
    isPwd,
};

